import javax.security.sasl.SaslClient;
import java.util.Scanner;

public class TestDemo {
    //汉诺塔问题_递归
    public static int Hanoi(int n){
        if(1==n){
            return 1;
        }
        return 2 * Hanoi(n - 1) + 1;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请问塔中有多少个圆盘?");
        int n = scanner.nextInt();
        System.out.println("共需要移动" + Hanoi(n) + "次");
        scanner.close();
    }
    //求阶乘_递归
    public static int recursionFactorial(int n){
        if(n == 1){
            return 1;
        }
        return n * recursionSum(n - 1);
    }
    public static void main13(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(recursionFactorial(num));
        scanner.close();
    }
    //求和_递归
    public static int recursionSum(int n){
        if(n == 1){
            return 1;
        }
        return n * recursionSum(n - 1);
    }
    public static void main12(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(recursionSum(num));
        scanner.close();
    }
    //求和_重载
    public static int getSum(int x,int y){
        return x + y;
    }
    public static double getSum(double x,double y,double z){
        return x + y + z;
    }
    public static void main11(String[] args) {
        System.out.println("两个整数之和为"+getSum(16, 4));
        System.out.println("三个小数之和为"+getSum(1.8, 2.5, 3.6));
    }
    //求最大值_重载
    public static int theMax(int x,int y){
        return Math.max(x,y);
    }
    public static double theMax(double x,double y){
        return Math.max(x,y);
    }
    public static void main10(String[] args) {
        System.out.println("最大值为"+theMax(5, 9));
        System.out.println("最大值为" + theMax(5.9, 9.5));
    }
    //求最大值
    public static int theMaxTwo(int x,int y){
        return Math.max(x,y);
    }
    public static int theMaxThree(int a,int b,int c){
        return theMaxTwo(theMaxTwo(a,b),c);
    }
    public static void main9(String[] args) {
        System.out.println("请输入三个数:>");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int max = theMaxThree(a,b,c);
        System.out.println("最大值为" + max);
    }
    //奇偶排序
    public static void main8(String[] args) {
        int[] arr = {2,3,6,9,1,4,5,8};
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            while(left < right && arr[left] % 2 != 0){
                left++;
            }
            while(left < right && arr[right] % 2 == 0){
                right--;
            }
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;//交换
        }
        for(int i = 0;i < arr.length;i++){
            System.out.print(arr[i] + " ");
        }
    }
    //求阶乘+阶乘和_以前写过了
    public static int factorial(int n) {
        int i = 0;
        int sum = 1;
        for (i = 1; i <= n; i++) {
            sum *= i;
        }
        return sum;
    }
    public static void factorialSum(int n){
        int sum = 0;
        for(int i = 1;i <= n;i++){
            sum += factorial(i);
            if(i == n){
                System.out.println(n+"的阶乘为"+factorial(i));
            }
        }
        System.out.println(n+"的阶乘和为"+sum);
    }
    public static void main7(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        factorialSum(num);
        scanner.close();
    }
    //寻找独数
    public static void main6(String[] args) {
        int[] arr={1,2,3,4,5,4,3,2,1};
        int sum = 0;
        for(int i = 0;i < arr.length;i++){
            sum ^= arr[i];
        }
        System.out.println(sum);
    }
    //二进制序列_奇偶排序
    public static void binaryParitySort(int n){
        int i = 0;
        int bin = 0;
        for(i = 31;i >= 0;i-=2){
            bin = n >>> i;
            bin &= 1;
            System.out.print(bin+" ");
        }
        System.out.println();
        for(i = 30;i >= 0;i-=2){
            bin = n >>> i;
            bin &= 1;
            System.out.print(bin+" ");
        }
    }
    public static void main5(String[] args) {
        binaryParitySort(8);
    }
    //青蛙跳台阶
    public static int frogJump(int n){
        if(n == 1||n == 2){
            return n;
        }
        int f1 = 1;
        int f2 = 2;
        int f3 = 0;
        for(int i = 3;i <= n;i++){
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static void main4(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int numberOfSteps = scanner.nextInt();
        System.out.println(frogJump(numberOfSteps));
        scanner.close();
    }
    //斐波那契数列_迭代版
    public static int fibIteration(int n){
        if(n == 1||n == 2){
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for(int i = 3;i <= n;i++){
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    //斐波那契数列_递归版
    public static int fibRecursion(int n){
        if(n == 1||n== 2){
            return 1;
        }
        return fibRecursion(n - 1) + fibRecursion(n - 2);
    }
    public static void main3(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int fib = scanner.nextInt();
        System.out.println("递归版"+fibRecursion(fib));
        System.out.println("迭代版" + fibIteration(fib));
        scanner.close();
    }
    //递归数位和
    public static int recursiveDigitSum(int n){
        if(n < 10){
            return n;
        }
        return n % 10 + recursiveDigitSum(n / 10);
    }
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int sum = recursiveDigitSum(num);
        System.out.println(num + "的各数位和为" + sum);
        scanner.close();
    }
    //递归打印位
    public static void recursivePrintBit(int n){
        if(n <= 9){
            System.out.print(n % 10+" ");
        }
        else{
            recursivePrintBit(n / 10);
            System.out.print(n % 10+" ");
        }
    }
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        recursivePrintBit(num);
        scanner.close();
    }
}
